set more off

/* This file replicates the results in Table 4, Chegere, Eggert & Söderbom (CES) */ 

/*cd "C:\xxx....*/

discard 
clear all

use "CES2019_data.dta" 

keep if wave==1 & est_sample==1

/* this program uses wild bootstrap procedure for tests (storing relevant statistics in a matrix)*/

prog def btf2 
boottest {bagstreatment}, cluster (villagecode) reps(999) seed (906453) nogr
mat junk=(_b[bagstreatment], r(p))
boottest {traintreatment}, cluster (villagecode) reps(999) seed (906454) nogr
mat junk=(junk, _b[traintreatment], r(p))
boottest {sex}, cluster (villagecode) reps(999) seed (906455) nogr
mat junk=(junk, _b[sex], r(p))
boottest {age}, cluster (villagecode) reps(999) seed (906456) nogr
mat junk=(junk, _b[age], r(p))
boottest {schooling}, cluster (villagecode) reps(999) seed (906457) nogr
mat junk=(junk, _b[schooling], r(p))
boottest {activeworkers}, cluster (villagecode) reps(999) seed (6453) nogr
mat junk=(junk, _b[activeworkers], r(p))
boottest {wealth}, cluster (villagecode) reps(999) seed (6454) nogr
mat junk=(junk, _b[wealth], r(p))
boottest {maizeexperienceyears}, cluster (villagecode) reps(999) seed (6455) nogr
mat junk=(junk, _b[maizeexperienceyears], r(p))
boottest {trained}, cluster (villagecode) reps(999) seed (6456) nogr
mat junk=(junk, _b[trained], r(p))
boottest {bagstreatment=traintreatment}, cluster (villagecode) reps(999) seed (6457) nogr
mat junk=(junk, _b[bagstreatment]-_b[traintreatment], r(p))
end


* dummy = 1 if any sales, 0 otherwise 
ge soldM=amountsold>0
replace soldM=0 if amountsold==.

* sales in kg, including zeros
ge amsoldz=amountsold
replace amsoldz=0 if soldM==0

* sales early and late
ge soldearly=amountsoldin3months
replace soldearly=0 if soldM==0

ge soldlate=(amountsold-amountsoldin3months)
replace soldlate=0 if soldM==0

* sales diff, late vs early
ge Dst=(soldlate-soldearly)

* share sold early (not defined for non-sellers as denominator will be zero
ge early_late=soldearly/(soldearly+soldlate)


/* Table 4: Impact on sales patterns and price (note: infererence is not based on the std errors reported here)  */

local k=0
foreach yvar in amsoldz soldearly soldlate Dst early_late highestprice lowestprice averageprice {
	local k=`k'+1
	sum `yvar'
	reg `yvar' bagstreatment traintreatment sex age schooling activeworkers wealth maizeexperienceyears trained, cluster(villagecode)
	btf2

if `k'==1{
	mat JUNK=junk'
}
	else {
	mat JUNK=(JUNK,junk')
}	
	}

	
local k=0
foreach yvar in amsoldz soldearly soldlate Dst early_late highestprice lowestprice averageprice {
	local k=`k'+1
randcmd ((bagstreatment traintreatment) reg `yvar' bagstreatment traintreatment sex age schooling activeworkers wealth maizeexperienceyears trained, cluster(villagecode)), treatvars(bagstreatment traintreatment)  groupvar(village)

mat junkx=e(RCoef)
mat junk=junkx[1..2,6]

if `k'==1{
	mat JUNKx=junk
}
	else {
	mat JUNKx=(JUNKx,junk)
}	

}

/* the JUNK matrix has stored the coefficient estimates and the w-bootstrapped p-values. the JUNKx matrix has stored the randomization-t randomized p-values */
mat list JUNK
mat list JUNKx
exit
